package com.foo.ui_demo_widge;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;


import java.util.Calendar;

public class PickDialogActivity extends AppCompatActivity
        implements DatePickerDialog.OnDateSetListener , TimePickerDialog.OnTimeSetListener {
private Button btndata;
private Button btntime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_dialog);
       btndata= findViewById(R.id.btn_data);
               btndata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerFragment fragment = new DatePickerFragment();
                fragment.show(getSupportFragmentManager(),"DatePicker");
            }
        });
        btntime=findViewById(R.id.btn_data_pick);
        btntime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimePickerFragment fragment = new TimePickerFragment();
                fragment.show(getSupportFragmentManager(),"TimePicker");
            }
        });
    }

    @Override
    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
        btndata.setText(i+"年"+(i1+1)+"月"+i2+"日");

    }

    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        btntime.setText(i+"点"+i1+"分");
    }

    public static class DatePickerFragment extends DialogFragment{
        @NonNull

        @Override
        public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
            final Calendar c = Calendar.getInstance();
            int year=c.get(Calendar.YEAR);
            int month=c.get(Calendar.MONTH);
            int day=c.get(Calendar.DAY_OF_MONTH);
           PickDialogActivity activity = (PickDialogActivity) getActivity();
            return new DatePickerDialog(getActivity(),activity,year,month,day);
        }
    }


    public static class TimePickerFragment extends DialogFragment{
        @NonNull

        @Override
        public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
            final Calendar c = Calendar.getInstance();
            int hour=c.get(Calendar.HOUR_OF_DAY);
            int minute=c.get(Calendar.MINUTE);
            PickDialogActivity activity = (PickDialogActivity) getActivity();

            return new TimePickerDialog
                    (getActivity(),
                            activity,hour,minute, DateFormat.is24HourFormat(activity));
        }
    }
}